#!/usr/bin/env bash
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
GREEN_PS3=$'\e[0;32m'
ORANGE_PS3=$'\e[0;33m'
WHITE='\033[0;37m'

if [ -z "${KUBECONFIG}" ]; then
    export KUBECONFIG=~/.kube/config
fi

#Namespace
NAMESPACE=$(kubectl get sts --all-namespaces | grep prometheus-k8s | cut -d " " -f1)

kctl() {
    kubectl --namespace "$NAMESPACE" "$@"
}

#Set namespace in various resources
for n in $(egrep -lir --include=*.{yaml,sh} "CUSTOM_NAMESPACE" .); do
  sed -i -e 's,CUSTOM_NAMESPACE,'"$NAMESPACE"',g' $n
done

kctl delete -f manifests/node-exporter
kctl delete -f manifests/kube-state-metrics
find manifests/grafana -type f ! -name grafana.pvc.yaml -exec kubectl --namespace "$NAMESPACE" delete -f {} \;
find manifests/prometheus -type f ! -name prometheus-k8s-rbac.yaml -exec kubectl --namespace "$NAMESPACE" delete -f {} \;
kubectl delete -f manifests/prometheus/prometheus-k8s-rbac.yaml
kctl delete -f manifests/alertmanager
kubectl delete -f manifests/k8s/self-hosted

# Hack: wait a bit to let the controller delete the deployed Prometheus server.
sleep 5

find manifests/prometheus-operator -type f ! -name prometheus-operator-cluster-role-binding.yaml -exec kubectl --namespace "$NAMESPACE" delete -f {} \;
kubectl delete -f manifests/prometheus-operator/prometheus-operator-cluster-role-binding.yaml

kctl delete secret grafana-credentials

kctl delete svc alertmanager-operated

kctl delete svc prometheus-operated

kubectl delete clusterrolebinding admin-binding

#delete created custom resource definitions

kubectl get crd -o 'jsonpath={.items[*].metadata.name}' | xargs -n 1 | fgrep coreos.com | xargs kubectl delete crd

#manually remove persistent volumes instructions
echo -e "${GREEN}The persistent volumes for Grafana and Prometeus were not removed!"
echo -e "${GREEN}To remove them, please run te following command:"
echo -e "${WHITE}kubectl get pvc -n ${NAMESPACE} | grep 'grafana\|prometheus' | cut -d '" "'  -f 1 | xargs -n 1 kubectl delete pvc -n ${NAMESPACE}"

#clean sed generated files
find . -name "*.yaml-e" -exec rm -rf {} \;

git checkout -- .